#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mapwidget.h"
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QPixmap>
#include <QScreen>
#include <QFileDialog>
#include <QPixmap>
#include <QScreen>
#include <qapplication.h>
#include <qpainter.h>
#include <QMessageBox>
#include <QPixmap>
#include <QScreen>
#include <QFileDialog>
#include <QDesktopServices>
#include <QClipboard>
#include <QScrollBar>
#include <QApplication>
#include <QLocale>
#include <QTranslator>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // ui->widget_2 = tmp;

    // QTimer *timer = new QTimer(this);
    // connect(timer, &QTimer::timeout, this, &MainWindow::saveScreenshot);
    // timer->start(10000); // 每1秒

    QTimer *timer_ros_vison = new QTimer(this);
    connect(timer_ros_vison, &QTimer::timeout, this, &MainWindow::vison_ros_status);
    timer_ros_vison->start(1000); // 每1秒

    connect(&map, SIGNAL(mySignal()), this, SLOT(mySlot()));
    connect(ui->mapWidget,SIGNAL(mySignal()),this, SLOT(SlotSavePic()));
}

//完成寻路之后，保存图片
void MainWindow::SlotSavePic()
{
    saveScreenshot();
}

void MainWindow::mySlot()
{
    // 槽函数的实现
    qDebug() << "槽函数被调用了";
    ui->mapWidget->zuoshang.x =  map.zuoshangX;
    ui->mapWidget->zuoshang.y = map.zuoshangY;

    ui->mapWidget->youshang.x = map.youshangX;
    ui->mapWidget->youshang.y = map.youshangY;

    ui->mapWidget->zuoxia.x = map.zuoxiaX;
    ui->mapWidget->zuoxia.y = map.zuoxiaY;

    ui->mapWidget->youxia.x = map.youxiaX;
    ui->mapWidget->youxia.y = map.youxiaY;

    ui->mapWidget->transzuobiao();//更新边界
    // saveScreenshot();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::vison_ros_status()
{
    // 将文本内容逐行添加到textEdit中，保持最多64行显示
    int startPos = 0;
    int endPos = 0;
    int lineCount = ui->textEdit->document()->lineCount();


    // for (const QString &text : tmp.vison_ros_string.mid(startPos))
    // {
    //     ui->textEdit->append(text);
    // }
    ui->mapWidget->vison_ros_string = mysqlOP.queryData();
    ui->textEdit->append(ui->mapWidget->vison_ros_string);
    // 将滚动条滚动到最底部
    ui->textEdit->verticalScrollBar()->setValue(ui->textEdit->verticalScrollBar()->maximum());

}

void MainWindow::saveScreenshot()
{
#if 1
    QDateTime currentDateTime = QDateTime::currentDateTime();
    QString localTime = currentDateTime.toString("yyyy-MM-dd hh:mm:ss");
    QScreen *screen = QGuiApplication::primaryScreen();
    QPixmap pixmap = this->grab();

    pixmap.save(localTime + ".png");

#else
    // 获取屏幕
    QScreen *screen = QGuiApplication::primaryScreen();
    if (screen)
    {
        // 获取当前窗口的尺寸和位置
        QRect geometry = this->geometry();
        // 获取主屏幕对象
        QScreen *primaryScreen = QGuiApplication::primaryScreen();
        // 获取主屏幕的窗口对象
        QWindow *primaryWindow = primaryScreen->handle();
        // 获取主屏幕窗口的句柄
        WId handle = primaryWindow->winId();
        // 截取当前窗口的像素内容
        QPixmap screenshot = screen->grabWindow(handle,
                                               geometry.x(), geometry.y(),
                                               geometry.width(), geometry.height());

        // 保存截图到文件
        QString filePath = QFileDialog::getSaveFileName(this, tr("保存截图"), "screenshot.png", tr("Images (*.png)"));
        if (!filePath.isEmpty()) {
            screenshot.save(filePath);
        }
    }
#endif
    qDebug() << "截图完成";
}

//启动按钮
void MainWindow::on_pushButton_clicked()
{
    // ui->mapWidget->carStat = ui->mapWidget->carStatOld;
    ui->mapWidget->carStat =  2;
}

//停止按钮
void MainWindow::on_pushButton_2_clicked()
{
    ui->mapWidget->carStatOld = ui->mapWidget->carStat;//保存旧的状态
    ui->mapWidget->carStat = 4;//主车停止
}

//左转10度，破坏自动寻路
void MainWindow::on_pushButton_3_clicked()
{

}

//右转10度，破坏自动寻路
void MainWindow::on_pushButton_4_clicked()
{

}

//设置坐标参数按钮
void MainWindow::on_pushButton_15_clicked()
{
    if(map.isVisible())
        map.hide();
    else
        map.show();
}

//返回车间
void MainWindow::on_pushButton_12_clicked()
{
    ui->mapWidget->carStat = 3;
    ui->mapWidget->carDirection = 270;
    ui->mapWidget->xuangengStat = 0;//旋耕空闲
}

//主车停止
void MainWindow::on_pushButton_5_clicked()
{
    ui->mapWidget->carStat = 0;//进入空闲状态
}

//耕耘: 警告：只有自动化运行到耕耘状态时，然后点击了空闲，后才能点击该按钮
void MainWindow::on_pushButton_10_clicked()
{
    ui->mapWidget->carStat = 1;//耕耘状态
}

//去耕耘: 警告:必须在开始位置才能点击，否则出现不可预计错误
void MainWindow::on_pushButton_11_clicked()
{
    ui->mapWidget->carStat = 2;//去耕耘状态
}

//检测按钮，检测车辆当前状态：车辆运行状态，车辆旋耕状态，车辆的电量
//并显示到右边大屏上
void MainWindow::on_pushButton_14_clicked()
{
    ui->textEdit->append(mysqlOP.queryData());

    QString tmp = "车辆状态：" + QString::number(ui->mapWidget->carStat) + ";旋耕状态:" + QString::number(ui->mapWidget->xuangengStat) + ";电量:" + QString::number(ui->mapWidget->engnge);
    ui->textEdit->append(tmp);
    // 将滚动条滚动到最底部
    ui->textEdit->verticalScrollBar()->setValue(ui->textEdit->verticalScrollBar()->maximum());
}

//耕耘空闲
void MainWindow::on_pushButton_7_clicked()
{
    ui->mapWidget->xuangengStat = 0;
}
//旋耕停止， 旋耕机的停止和空闲都不损耗电量
void MainWindow::on_pushButton_6_clicked()
{
    ui->mapWidget->xuangengStat = 0;
}

//去充电，就是返回车间充电
void MainWindow::on_pushButton_13_clicked()
{
    ui->mapWidget->carStat = 3;//离开耕地
    ui->mapWidget->carDirection = 270;//方向向上
    ui->mapWidget->xuangengStat = 0;//旋耕空闲
}



//预设坐标选择
void MainWindow::on_pushButton_16_clicked()
{
    int index = ui->spinBox->value();
    if(index <=0)
    {
        QMessageBox::information(this, "警告", "选择的不符合要求");
        return;
    }
    mysqlOP.queryPosition1(index);

    ui->mapWidget->zuoshang.x = mysqlOP.zuoshang.x;
    ui->mapWidget->zuoshang.y = mysqlOP.zuoshang.y;
    ui->mapWidget->youshang.x = mysqlOP.youshang.x;
    ui->mapWidget->youshang.y = mysqlOP.zuoshang.y;
    ui->mapWidget->zuoxia.x = mysqlOP.zuoxia.x;
    ui->mapWidget->zuoxia.y = mysqlOP.zuoxia.y;
    ui->mapWidget->youxia.x = mysqlOP.youxia.x;
    ui->mapWidget->youxia.y = mysqlOP.youxia.y;

    ui->mapWidget->transzuobiao();//刷新边界
}

